# encoding: utf-8

import os
from webtest import TestApp
from main import application
import unittest

app = TestApp(application())

test_data = {
  'email': 'mujo@example.org'
}

def login(email):
  os.environ['USER_EMAIL'] = email

class AcceptanceTests(unittest.TestCase):
  def setUp(self):
    os.environ['USER_EMAIL'] = ''
  
  def test_is_index(self):
    response = app.get('/')
    assert 'MojProjekat' in str(response)

  def test_login_link_present(self):
    response = app.get('/')
    assert response.html.find(id='login').name == "a"
  
  def test_user_logout_link_present(self):
    login(test_data['email'])
    response = app.get('/')
    assert response.html.find(id='logout').name == "a"
  
  def test_new_project_link_present(self):
    login(test_data['email'])
    response = app.get('/')
    assert response.html.find(id='new_project').name == "a"
  
  def test_new_project_link_click(self):
    login(test_data['email'])
    response = app.get('/')
    response = response.click(linkid='new_project')
    assert response.form.action == \
      '/project/new'
  
  def test_new_project_create(self):
    login(test_data['email'])
    response = app.get('/project/new')
    form = response.form
    form['name'] = 'Testni projekat'
    response = form.submit()
    response = response.follow()
    assert 'Projekat je uspješno napravljen' in str(response)
  
  def test_unauthorized_user_cant_access_new_project(self):
    response = app.get('/project/new')
    assert response.status == '302 Moved Temporarily'
